# -*- coding: utf-8 -*-
# @File  : app03.py
# @Author:  liubinyu
# @Time: 2024/5/19 12:50

from typing import Union, List, Optional
from fastapi import APIRouter
from pydantic import BaseModel, Field, ValidationError, validator
import uvicorn
from datetime import date

app03 = APIRouter()


class Addr(BaseModel):
    province: str
    city: str

class User(BaseModel):
    name: str = 'root'
    age: int = Field(default=0, lt=100, gt=0)
    birth: Optional[date] = None
    friends: List[int] = []
    description: Union[str, None] = None

    # addr: Union[Addr, None] = None  # 类型嵌套

    @validator('name')
    def name_must_alpha(cls, v):
        assert v.isalpha(), 'name must be alpha'
        return v

class Data(BaseModel):  # 类型嵌套
    users: List[User]

@app03.post("/user")
async def user(user:User):
    print(user,type(user))
    print(user.name,user.birth)
    print(user.dict())
    return user